/**
 * 
 */
package org.huanglei.account.view;

import org.huanglei.account.MyApplication;
import org.huanglei.account.R;
import org.huanglei.account.provider.LocalDataProvider;
import org.huanglei.account.utils.DatabaseToSD;
import org.huanglei.account.view.base.TitleBarActivity;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;


/**
 * 数据备份
 * @author huanglei
 * @data Jul 18, 2012
 */
public class AccountBackupActivity extends TitleBarActivity {
	
    private ProgressBar rectangleProgressBar,circleProgressBar;  
    private Button button;  
      
    protected static final int STOP = 0x10000;  
    protected static final int NEXT = 0x10001;  
    private int iCount = 0;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.huanglei.account.view.base.TitleBarActivity#getContentView()
	 */
	@Override
	protected int getContentView() {
		return R.layout.account_backup;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.huanglei.account.view.base.TitleBarActivity#init()
	 */
	@Override
	protected void init() {
		
		MyApplication.getInstance().addActivity(this);
		
		initTitleBar();
		
		backup();
	}

	/**
	 * 初始化标题
	 */
	private void initTitleBar() {
		setBarTitle(getResources().getString(R.string.account_backup));
		showBarView(TitleBarView.TBV_BACK, CL_BACK);
		showBarView(TitleBarView.TBV_HOME,
				CL_HOME.setObject(DashboardActivity.class));
	}
	
	
	private void backup(){
		
		rectangleProgressBar = (ProgressBar)findViewById(R.id.rectangleProgressBar);  
		circleProgressBar = (ProgressBar)findViewById(R.id.circleProgressBar);  
		button = (Button)findViewById(R.id.button); 
		
		rectangleProgressBar.setIndeterminate(false);  
		circleProgressBar.setIndeterminate(false);
		
		button.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {

				rectangleProgressBar.setVisibility(View.VISIBLE);  
				circleProgressBar.setVisibility(View.VISIBLE);  
				      
				rectangleProgressBar.setMax(100);  
				rectangleProgressBar.setProgress(0);  
				circleProgressBar.setProgress(0);
				
                //创建一个线程,每秒步长为5增加,到100%时停止  
                Thread thread = new Thread(new Runnable() {

					private Context AccountBackupActivity;

					@Override
					public void run() {
						for(int i=0 ; i < 20; i++){ 
							
							try{  
								
                                DatabaseToSD dsd = new DatabaseToSD();
                                dsd.copyDatabaseToSD(this.AccountBackupActivity, LocalDataProvider.DATABASE_NAME);
								
								iCount = (i + 1) * 5;  
                                Thread.sleep(1000);
                                
                                if(i == 19){  
                                    Message msg = new Message();  
                                    msg.what = STOP;  
                                    mHandler.sendMessage(msg);  
                                    break;  
                                }else{
                                	Message msg = new Message();  
                                    msg.what = NEXT;  
                                    mHandler.sendMessage(msg);
                                }
								
							}catch(Exception e) {  
                				e.printStackTrace();  
							}
							
						}
						
					}
				
                });
				
				thread.start();
			}
			
		});
		
	}
	   //定义一个Handler  
    private Handler mHandler = new Handler(){
    	
        public void handleMessage(Message msg){  
            switch (msg.what) {  
            	case STOP:  
            		rectangleProgressBar.setVisibility(View.GONE);  
            		circleProgressBar.setVisibility(View.GONE);  
            		Thread.currentThread().interrupt();  
            		break;  
            	case NEXT:  
            		if(!Thread.currentThread().isInterrupted()){  
            			rectangleProgressBar.setProgress(iCount);  
            			circleProgressBar.setProgress(iCount);  
            		}  
            	break;  
            }  
        }  
    };

}
